﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Cnblogs.Service;
using System.Collections.ObjectModel;

namespace Cnblogs
{
    public partial class CommentsPage : PhoneApplicationPage
    {
        private int _commentPage = 1;
        private bool _isLoaded = false;
        private string _postId = "";
        private ServiceType _type = ServiceType.PostComment;
        ObservableCollection<Entry> Source = new ObservableCollection<Entry>();
        public CommentsPage()
        {
            InitializeComponent();
            lbx_comment.ItemsSource = Source;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {

            if (!_isLoaded)
            {
                _isLoaded = true;
                if (NavigationContext.QueryString.ContainsKey("title"))
                {
                    tbk_title.Text = NavigationContext.QueryString["title"] + " 的评论";
                }
                if (NavigationContext.QueryString.ContainsKey("id"))
                {
                    _postId = NavigationContext.QueryString["id"];

                    if (NavigationContext.QueryString["type"] == "news")
                    {
                        _type = ServiceType.NewsComment;
                        loadComments(_postId, ServiceType.NewsComment);
                    }
                    else
                    {
                        _type = ServiceType.PostComment;
                        loadComments(_postId, ServiceType.PostComment);
                    }
                }
            }
            base.OnNavigatedTo(e);
        }

        private void loadComments(string postId, ServiceType type)
        {
            GlobalIndicator.Instance.StartBusy(Constants.L_GettingData);
            FeedService.Request(type, new NewsData
            {
                Id = postId,
                PageIndex = _commentPage,
                PageSize = GlobalFunc.PageSize
            }, (obj, isSuccess) =>
                {
                    GlobalIndicator.Instance.StopBusy();
                    Deployment.Current.Dispatcher.BeginInvoke(delegate
                    {
                        var items = obj as NewsFeed;
                        if (items != null && items.Items != null)
                        {
                            btn_loadmore.Visibility = (items.Items.Length == GlobalFunc.PageSize) ? Visibility.Visible : Visibility.Collapsed;
                            foreach (var item in items.Items)
                            {
                                Source.Add(item);
                            }
                        }
                    });
                });
        }

        private void imgAvatar_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var img = sender as Image;
            if (img != null)
            {
                var entry = img.DataContext as Entry;
                if (entry != null)
                {
                    NavigationService.Navigate(new Uri(Constants.P_BloggerPage + "?blogapp=" + entry.BlogApp + "&name=" + entry.AuthorInfo.Name, UriKind.Relative));
                }
            }
        }

        private void btn_loadmore_click(object sender, RoutedEventArgs e)
        {
            _commentPage++;
            loadComments(_postId, _type);
        }
    }
}